#include "paramsetandread.h"

ParamSetAndRead::ParamSetAndRead()
{
    itemRead = new QPushButton(tr("召测"));
    itemSend = new QPushButton(tr("设置"));
    passwordInput = new QLineEdit();

    paramList = new QListWidget();
    param09F1 = new QListWidgetItem();
    param09F1->setText(tr("软件版本"));

    paramF3 =new QListWidgetItem();
    //paramF3->setFlags(Qt::ItemIsSelectable);
#ifdef HONGHAO212
    paramF3->setText(tr("省和区块链主站IP和端口--鸿浩专用"));
    paramF3->setTextColor(Qt::red);
#else
    paramF3->setText(tr("主站IP和端口"));
#endif
    paramF7 =new QListWidgetItem();
    paramF7->setText(tr("终端IP和端口"));
   // paramF7->setFlags(Qt::ItemIsSelectable);

    paramF16 =new QListWidgetItem();
    paramF16->setText(tr("用户名和密码"));
#ifdef HONGHAO212
    paramF199=new QListWidgetItem(tr("读取IMEI ICCID---鸿浩用"));  //读取imei iccid
    paramF199->setTextColor(Qt::red);
#else
    paramF199=new QListWidgetItem(tr("读取IMEI ICCID"));  //读取imei iccid
#endif
    paramF796Monitor=new QListWidgetItem(tr("报文监控开关"));  //设置F796报文监控
    paramF796MES=new QListWidgetItem(tr("读取MES"));  //读取796 mes码。
    paramUpdate = new  QListWidgetItem(tr("升级模块程序"));  //376 协议升级
#ifdef HONGHAO212
    paramF803   =  new  QListWidgetItem(tr("市平台,控制平台---鸿浩专用"));  //鸿浩读取第三和第四条连接
    paramF803->setTextColor(Qt::red);
#else
    paramF803   =  new  QListWidgetItem(tr("F803"));  //鸿浩读取第三和第四条连接
#endif
#ifdef HONGHAO212
    paramF796MN = new QListWidgetItem(tr("读取MN号---鸿浩专用"));  //读取796 MN号。
    paramF796MN->setTextColor(Qt::red);
    paramF796ERTUADDR = new QListWidgetItem(tr("读取tn号---鸿浩专用"));  //读取796 MN号。
    paramF796ERTUADDR->setTextColor(Qt::red);
#else
    paramF796MN = new QListWidgetItem(tr("读取MN号"));  //读取796 MN号。
    paramF796ERTUADDR = new QListWidgetItem(tr("读取终端地址 "));  //读取796 MN号。
#endif

    paramF796VoltageCurrent =   new QListWidgetItem(tr("读取电压电流倍率---鸿浩用"));  //读取796 MN号。
    paramF796VoltageCurrent->setTextColor(Qt::red);


    paramList->addItem(param09F1);
    paramList->addItem(paramF3);
    paramList->addItem(paramF7);
    paramList->addItem(paramF16);
    paramList->addItem(paramF199);
    paramList->addItem(paramF796Monitor);
    paramList->addItem(paramF796MES);
    paramList->addItem(paramUpdate);
    paramList->addItem(paramF803);
    paramList->addItem(paramF796MN);
    paramList->addItem(paramF796ERTUADDR);
    paramList->addItem(paramF796VoltageCurrent);

     param09F1WidgetInit(); //09F1  读软件版本
     paramF3WidgetInit();    //F3 参数初始化显示页面。
     paramF7WidgetInit();    //F7 参数初始化显示页面。
     paramF16WidgetInit();    //F16 用户名 密码。
     paramF796_16WidgetInit();    //F796  _16项 用户名 密码。
     paramF799WidgetInit();
     paramF796_24WidgetInit();   //F796  mes 码读取。
     Update376_WidgetInit();   //376协议升级界面。
     paramF803WidgetInit();    //F803 参数初始化显示页面。
     paramF796_182WidgetInit();   //F796  MN号读取。
    paramF796_1WidgetInit();   //F796  MN号读取。
    paramF796_18_36WidgetInit();   //F796 电压电流倍率。



     stack = new QStackedLayout();
     stack ->addWidget(param09F1Widget);
     stack->addWidget(paramF3Widget);
     stack->addWidget(paramF7Widget);
     stack ->addWidget(paramF16Widget);
     stack ->addWidget(paramF799Widget);
     stack ->addWidget(paramF796_16Widget);
     stack ->addWidget(paramF796_24Widget);
     stack ->addWidget(Update376Widget);
     stack ->addWidget(paramF803Widget);
     stack ->addWidget(paramF796_182Widget);
     stack ->addWidget(paramF796_1Widget);
     stack ->addWidget(paramF796_18_36Widget);


     CtrlbuttonLayout = new QHBoxLayout();
     setItemLayout = new QHBoxLayout();
     setMainLayout  = new QVBoxLayout();

      CtrlbuttonLayout->addWidget(itemRead);
      CtrlbuttonLayout->addWidget(itemSend);
      CtrlbuttonLayout->addWidget(passwordInput);
      CtrlbuttonLayout->addStretch(3); //设置弹簧

      setItemLayout->addWidget(paramList);
      setItemLayout->addLayout(stack);
      setItemLayout->setStretchFactor(paramList,1);
      setItemLayout->setStretchFactor(stack,3);
      setMainLayout->addLayout(CtrlbuttonLayout);
      setMainLayout->addLayout(setItemLayout);
     connect(paramList,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));
     connect (itemSend,SIGNAL(clicked()),this,SLOT(paramWriteSlot()));
     connect (itemRead,SIGNAL(clicked()),this,SLOT(paramReadSlot()));
     connect (this,SIGNAL(flushPageShow()),this,SLOT(updatePageShowSlot()));
     setLayout(setMainLayout);   //展示界面。
     resize(1000,500);
     setWindowTitle("参数设置与查询");

     timerUpdate = startTimer(1000*5);

     this->setAttribute(Qt::WA_QuitOnClose, false);
}

void ParamSetAndRead::timerEvent(QTimerEvent *e)
 {
     if(e->timerId()==timerUpdate)
     {
        if(intoUpdateState == 0xAA)
        {
                if(retransmitCnt++ >2)
                {
                        qDebug()<<"overtime need to retransmite";
                        make_376update_frame();
                }
                if(retransmitCnt>6)
                {
                       intoUpdateState =0;
                      QMessageBox::warning(this, "Warning!", "升级失败");
                }
        }
     }
}
void ParamSetAndRead::test()
{
    qDebug()<< "监听信号是";
}
void ParamSetAndRead::updatePageShowSlot()
{
     int pos  =0;
    switch(serial_channel.need_show_AFN)
    {
        case 0x9:
        switch (serial_channel.need_show_FN  )
        {
                case 1:
            param09F1WidgetShow();
                    break;
        }
            break;
        case 0xA:
        switch (serial_channel.need_show_FN  )
        {
                case 3:
                    paramF3WidgetShow();
                    break;
                case 7:
                    paramF7WidgetShow();
                    break;
                case 16:
                    paramF16WidgetShow();
                    break;
                case 796:
                    if(serial_channel.need_show_subFn == 1)
                    {
                        paramF796_1WidgetShow();
                    }
                    if(serial_channel.need_show_subFn == 16)
                    {
                        paramF796_16WidgetShow();
                    }
                    else if(serial_channel.need_show_subFn == 24)
                    {
                        paramF796_24WidgetShow();
                    }
                    else if(serial_channel.need_show_subFn == 182)
                    {
                        paramF796_182WidgetShow();
                    }
                    else if(serial_channel.need_show_subFn == 18)   //电压电流倍率
                    {
                        paramF796_18_36WidgetShow();
                    }
                    break;
                 case 803:
                        paramF803WidgetShow();
                    break;
                default:
                    break;
        }
            break;
        case 0x0C:
            switch (serial_channel.need_show_FN  )
            {
                    case 799:
                    paramF799WidgetShow();
                    break;
            default:
                break;
              }
            break;
         case 0x0F:  //报文升级判断
            pos = 12;
             pos +=2;
             pos +=2;
            if((serial_channel.recevice_frame_waite[pos] == 0x1)&&(serial_channel.recevice_frame_waite[pos+1] == 0x0) )
            {
                pos +=2;
                unsigned int get_cur_seg =  (unsigned int )(serial_channel.recevice_frame_waite[pos]&0xFF) + (unsigned int )((serial_channel.recevice_frame_waite[pos+1]&0xFF)<<8)+\
                        + (unsigned int )((serial_channel.recevice_frame_waite[pos+2]&0xFF)<<16)+  (unsigned int )((serial_channel.recevice_frame_waite[pos+3]&0xFF)<<24);
                    if(get_cur_seg == file_cur_seg)
                    {
                        qDebug()<<"hello  "+QString("%1").arg(get_cur_seg,0,10)+"   "+QString("%1").arg(file_cur_seg,0,10)+" download is ok";
                        file_cur_seg++;
                        retransmitCnt =0;
                        intoUpdateState =0;
                        progressbar->setValue((file_cur_seg*100)/file_total_seg);
                        make_376update_frame();

                    }
                    else
                    {
                        qDebug()<<"heihie "+QString("%1").arg(get_cur_seg,0,10)+"   "+QString("%1").arg(file_cur_seg,0,10);
                    }
            }
                break;
    default:
            break;
    }
}

void ParamSetAndRead::param09F1WidgetInit()
{
    param09F1GridLayout = new QGridLayout();
    F109ertuAddr =new QLabel(tr("终端地址"));
    F109softVer =new QLabel(tr("软件版本"));
    F109ertuAddrEdit = new QLineEdit();
    F109softVerEdit = new QLineEdit();

    param09F1GridLayout->addWidget(F109ertuAddr,0,0);
    param09F1GridLayout->addWidget(F109ertuAddrEdit,0,1);
    param09F1GridLayout->addWidget(F109softVer,1,0);
    param09F1GridLayout->addWidget(F109softVerEdit,1,1);


    param09F1GridLayout->setColumnStretch(2,2);
    param09F1GridLayout->setRowStretch(2,2);
     param09F1Widget = new QWidget();      //这里必须要定义一个widget，否则后面connect出问题。
    param09F1Widget->setLayout(param09F1GridLayout);
}
void ParamSetAndRead::param09F1WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4A;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_READCFG;
   request.send_frame[pos++] = 0x60;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F1 &0xFF;
   request.send_frame[pos++] =( DT_F1>>8) &0xFF;
   request.pos = pos;
   protocol376.make376Frame(&request);
      //发送到串口。
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();
}
unsigned char ParamSetAndRead::bcd_to_hex(unsigned char data)
{
    unsigned char temp;

    temp = ((data>>4)*10 + (data&0x0f));
    return temp;
}
void ParamSetAndRead::param09F1WidgetShow()
{
    QString ertuAddr;
    int pos = 12;
    int port=0;
    if(serial_channel.recevice_frame_waite[pos] != 0x09)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x01)&&(serial_channel.recevice_frame_waite[pos+1] != 0x00) )
    {
        goto END;
    }
    pos+=2;
    pos+=4;
    ertuAddr.clear();
    for(int i=0;i<8;i++)
    {
        if(serial_channel.recevice_frame_waite[pos] !=0x00)
        {
            ertuAddr.append(serial_channel.recevice_frame_waite[pos+i]);
        }
    }
    pos+=8;
    pos+=4;

    F109ertuAddrEdit->setText(ertuAddr);

     F109softVerEdit->setText( QString("%1-%2-%3 ")
                .arg(bcd_to_hex(serial_channel.recevice_frame_waite[pos+2] ) & 0xFF)
                .arg(bcd_to_hex(serial_channel.recevice_frame_waite[pos+1] ) & 0xFF)
                .arg(bcd_to_hex(serial_channel.recevice_frame_waite[pos] ) & 0xFF)
                  );
END:
    serial_channel.need_show_flag =0;
}
void ParamSetAndRead::paramF3WidgetInit()
{
    //网址判断。
    QRegularExpression exp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
    QRegularExpressionValidator *ipVal = new QRegularExpressionValidator(exp);
    paramF3GridLayout =new QGridLayout();
#ifdef HONGHAO212
    F3MainIP = new QLabel(tr("省环保平台 IP"));
#else
        F3MainIP = new QLabel(tr("主用 IP"));
#endif
    F3MainPort = new QLabel(tr("端口"));
#ifdef HONGHAO212
    F3SecondIP = new QLabel(tr("固证平台(区块链)IP"));
#else
    F3SecondIP = new QLabel(tr("备用 IP"));
#endif
    F3SecondPort = new QLabel(tr("端口"));
    F3APN = new QLabel(tr("APN"));
#ifdef HONGHAO212
    F3MainIPEdit=new QLineEdit("218.11.13.63");
#else
    F3MainIPEdit=new QLineEdit("");
#endif
    F3MainIPEdit->setInputMask("000.000.000.000");
    F3MainIPEdit->setEchoMode(QLineEdit::Normal);
    F3MainIPEdit->setEnabled(true);
#ifdef     HONGHAO212
    F3MainPortEdit=new QLineEdit("5010");
#else
     F3MainPortEdit=new QLineEdit();
#endif
     F3MainPortEdit->setInputMask("0000");
    F3MainPortEdit->setMaxLength(5);
    F3MainPortEdit->setEchoMode(QLineEdit::Normal);
    F3MainPortEdit->setEnabled(true);
#ifdef     HONGHAO212
    F3SecondIPEdit=new QLineEdit("121.17.30.65");
#else
        F3SecondIPEdit=new QLineEdit();
#endif
     F3SecondIPEdit->setInputMask("000.000.000.000");
     F3SecondIPEdit->setEchoMode(QLineEdit::Normal);
     F3SecondIPEdit->setEnabled(true);
#ifdef      HONGHAO212
     F3SecondPortEdit=new QLineEdit("9606");
#else
      F3SecondPortEdit=new QLineEdit( );
#endif
      F3SecondPortEdit->setInputMask("00000");
     F3SecondPortEdit->setMaxLength(5);
    F3APNEdit=new QLineEdit("cmnet");
    F3ParamRestore = new QPushButton(tr("默认参数恢复"));
    paramF3GridLayout->addWidget(F3MainIP,0,0);
    paramF3GridLayout->addWidget(F3MainIPEdit,0,1);
    paramF3GridLayout->addWidget(F3MainPort,0,2);
    paramF3GridLayout->addWidget(F3MainPortEdit,0,3);

    paramF3GridLayout->addWidget(F3SecondIP,1,0);
    paramF3GridLayout->addWidget(F3SecondIPEdit,1,1);
    paramF3GridLayout->addWidget(F3SecondPort,1,2);
    paramF3GridLayout->addWidget(F3SecondPortEdit,1,3);

    paramF3GridLayout->addWidget(F3APN,2,0);
    paramF3GridLayout->addWidget(F3APNEdit,2,1);
    paramF3GridLayout->addWidget(F3ParamRestore,3,0);
    paramF3GridLayout->setColumnStretch(4,2);
    paramF3GridLayout->setRowStretch(4,2);
     paramF3Widget = new QWidget();      //这里必须要定义一个widget，否则后面connect出问题。
    paramF3Widget->setLayout(paramF3GridLayout);
    connect (F3ParamRestore,SIGNAL(clicked()),this,SLOT(F3ParamRestoreFun()));

}
void  ParamSetAndRead::paramF3WidgetSet()
{
    quint32 MainIP,SecondIP,MianPort,secondPort;
    QStringList ips = F3MainIPEdit->text().split(".");
    if(ips.size() == 4){
        MainIP =   ips.at(0).toInt()
                | ips.at(1).toInt() << 8
                | ips.at(2).toInt() << 16
                | ips.at(3).toInt() << 24;
    }
   ips = F3SecondIPEdit->text().split(".");
    if(ips.size() == 4){
        SecondIP =   ips.at(0).toInt()
                | ips.at(1).toInt() << 8
                | ips.at(2).toInt() << 16
                | ips.at(3).toInt() << 24;
    }
    MianPort = F3MainPortEdit->text().toInt();
    secondPort = F3SecondPortEdit->text().toInt();
    int pos =6;
     objRequest request;
     memset(request.send_frame,0,sizeof(request.send_frame));
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_SET;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F3 &0xFF;
    request.send_frame[pos++] =( DT_F3>>8) &0xFF;
    memcpy(request.send_frame+pos,(INT8U*)&MainIP,4);
    pos+=4;
    memcpy(request.send_frame+pos,(INT8U*)&MianPort,2);
    pos+=2;
    memcpy(request.send_frame+pos,(INT8U*)&SecondIP,4);
    pos+=4;
    memcpy(request.send_frame+pos,(INT8U*)&secondPort,2);
    pos+=2;
    char*  apn;
    QByteArray ba = F3APNEdit->text().toLatin1(); // must
    apn = ba.data();
    if(strlen(apn) <=16)  //apn 长度为16个字节。
    {
        memcpy(request.send_frame+pos,apn,strlen(apn));
    }
    pos+=16;
     pos+=16;   //PW 部分
     request.pos = pos;
     protocol376.make376Frame(&request);
     //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}
void  ParamSetAndRead::paramF3WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4A;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_QUERY;
   request.send_frame[pos++] = 0x60;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F3 &0xFF;
   request.send_frame[pos++] =( DT_F3>>8) &0xFF;
   request.pos = pos;
   protocol376.make376Frame(&request);
      //发送到串口。
   F3MainIPEdit->clear();
   F3SecondIPEdit->clear();
   F3MainPortEdit->clear();
   F3SecondPortEdit->clear();
   F3APNEdit ->clear();
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF3WidgetShow()
{
    QString  apn;
     int mainPort,secondPort;
    int pos = 12;
    if(serial_channel.recevice_frame_waite[pos] != 0x0A)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x04)&&(serial_channel.recevice_frame_waite[pos+1] != 0x00) )
    {
        goto END;
    }
    pos+=2;
    F3MainIPEdit->setText( QString("%1.%2.%3.%4")
                .arg((serial_channel.recevice_frame_waite[pos] ) & 0xFF)
                .arg((serial_channel.recevice_frame_waite[pos+1] ) & 0xFF)
                .arg((serial_channel.recevice_frame_waite[pos+2] ) & 0xFF)
                .arg(serial_channel.recevice_frame_waite[pos+3] & 0xFF));
    pos+=4;
    mainPort = ((int((uchar )serial_channel.recevice_frame_waite[pos+1]))<<8) +(uchar)serial_channel.recevice_frame_waite[pos] ;
    F3MainPortEdit->setText(QString("%1").arg(mainPort));
    pos+=2;
    F3SecondIPEdit->setText( QString("%1.%2.%3.%4")
                   .arg((serial_channel.recevice_frame_waite[pos] ) & 0xFF)
                   .arg((serial_channel.recevice_frame_waite[pos+1] ) & 0xFF)
                   .arg((serial_channel.recevice_frame_waite[pos+2] ) & 0xFF)
                   .arg(serial_channel.recevice_frame_waite[pos+3] & 0xFF));
      pos+=4;
      secondPort = ((uint((uchar )serial_channel.recevice_frame_waite[pos+1]))<<8) +(uchar)serial_channel.recevice_frame_waite[pos] ;
      F3SecondPortEdit->setText(QString("%1").arg(secondPort));
      pos+=2;

      apn.clear();
      for(int i=0;i<16;i++)
      {
          if(serial_channel.recevice_frame_waite[pos+i]!=0)
          {
              apn.append(serial_channel.recevice_frame_waite[pos+i]);
          }
      }
      F3APNEdit->setText(apn);
END:
    serial_channel.need_show_flag =0;
}
void ParamSetAndRead::paramF7WidgetInit()
{
     F7MainIP = new QLabel(tr("终端IP"));
     netMaskIP= new QLabel(tr("子网掩码"));
     gatewayIP= new QLabel(tr("网关地址"));
     listenPort= new QLabel(tr("监听端口"));
     F7MainIPEdit = new QLineEdit();
     F7MainIPEdit->setInputMask("000.000.000.000");
     F7MainIPEdit->setEchoMode(QLineEdit::Normal);
     F7MainIPEdit->setEnabled(true);
     netMaskIPEdit = new QLineEdit();
     netMaskIPEdit->setInputMask("000.000.000.000");
     netMaskIPEdit->setEchoMode(QLineEdit::Normal);
     netMaskIPEdit->setEnabled(true);
     gatewayIPEdit = new QLineEdit();
     gatewayIPEdit->setInputMask("000.000.000.000");
     gatewayIPEdit->setEchoMode(QLineEdit::Normal);
     gatewayIPEdit->setEnabled(true);
     listemPortEdit = new QLineEdit();
     listemPortEdit->setInputMask("00000");
     listemPortEdit->setEnabled(true);

     paramF7GridLayout =new QGridLayout();

     paramF7GridLayout->addWidget(F7MainIP,0,0);
     paramF7GridLayout->addWidget(netMaskIP,1,0);
     paramF7GridLayout->addWidget(gatewayIP,2,0);
     paramF7GridLayout->addWidget(listenPort,3,0);

     paramF7GridLayout->addWidget(F7MainIPEdit,0,1);
     paramF7GridLayout->addWidget(netMaskIPEdit,1,1);
     paramF7GridLayout->addWidget(gatewayIPEdit,2,1);
     paramF7GridLayout->addWidget(listemPortEdit,3,1);
     paramF7GridLayout->setColumnStretch(2,2);
     paramF7GridLayout->setRowStretch(4,2);
     paramF7Widget = new QWidget();      //这里必须要定义一个widget，否则后面connect出问题。
     paramF7Widget->setLayout(paramF7GridLayout);
}
void ParamSetAndRead::paramF7WidgetSet()
{
    quint32  mainIP=0,submaskIP=0,GateWayIP=0,listenPort=0;
          QStringList ips = F7MainIPEdit->text().split(".");
          if(ips.size() == 4){
              mainIP =   ips.at(0).toInt()
                      | ips.at(1).toInt() << 8
                      | ips.at(2).toInt() << 16
                      | ips.at(3).toInt() << 24;
          }
         ips = netMaskIPEdit->text().split(".");
          if(ips.size() == 4){
              submaskIP =   ips.at(0).toInt()
                      | ips.at(1).toInt() << 8
                      | ips.at(2).toInt() << 16
                      | ips.at(3).toInt() << 24;
          }
            ips = gatewayIPEdit->text().split(".");
          if(ips.size() == 4){
              GateWayIP =   ips.at(0).toInt()
                      | ips.at(1).toInt() << 8
                      | ips.at(2).toInt() << 16
                      | ips.at(3).toInt() << 24;
          }

     listenPort = listemPortEdit->text().toInt();
     int pos =6;
     objRequest request;
     memset(request.send_frame,0,sizeof(request.send_frame));
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_SET;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F7 &0xFF;
    request.send_frame[pos++] =( DT_F7>>8) &0xFF;
    memcpy(request.send_frame+pos,(INT8U*)&mainIP,4);
    pos+=4;
    memcpy(request.send_frame+pos,(INT8U*)&submaskIP,4);
      pos+=4;
    memcpy(request.send_frame+pos,(INT8U*)&GateWayIP,4);
      pos+=4;
      request.send_frame[pos++] = 0x00;  //不使用代理
      pos+=4;
      pos+=2;
      pos+=1;
      pos+=1;
      pos+=1;
      memcpy(request.send_frame+pos,(INT8U*)&listenPort,2);
       pos+=2;
      pos+=16;   //PW 部分
     request.pos = pos;
     protocol376.make376Frame(&request);
     //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF7WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4A;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_QUERY;
   request.send_frame[pos++] = 0x60;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F7 &0xFF;
   request.send_frame[pos++] =( DT_F7>>8) &0xFF;
   request.pos = pos;
  protocol376.make376Frame(&request);
      //发送到串口。
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF7WidgetShow()
{
    QString username,password;
    qDebug()<< "callback is run";
    int pos = 12;
    int port=0;
    if(serial_channel.recevice_frame_waite[pos] != 0x0A)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x40)&&(serial_channel.recevice_frame_waite[pos+1] != 0x01) )
    {
        goto END;
    }
    pos+=2;
    F7MainIPEdit->setText( QString("%1.%2.%3.%4")
                .arg((serial_channel.recevice_frame_waite[pos] ) & 0xFF)
                .arg((serial_channel.recevice_frame_waite[pos+1] ) & 0xFF)
                .arg((serial_channel.recevice_frame_waite[pos+2] ) & 0xFF)
                .arg(serial_channel.recevice_frame_waite[pos+3] & 0xFF));
    pos+=4;
    netMaskIPEdit->setText( QString("%1.%2.%3.%4")
                   .arg((serial_channel.recevice_frame_waite[pos] ) & 0xFF)
                   .arg((serial_channel.recevice_frame_waite[pos+1] ) & 0xFF)
                   .arg((serial_channel.recevice_frame_waite[pos+2] ) & 0xFF)
                   .arg(serial_channel.recevice_frame_waite[pos+3] & 0xFF));
      pos+=4;
      gatewayIPEdit->setText( QString("%1.%2.%3.%4")
                      .arg((serial_channel.recevice_frame_waite[pos] ) & 0xFF)
                      .arg((serial_channel.recevice_frame_waite[pos+1] ) & 0xFF)
                      .arg((serial_channel.recevice_frame_waite[pos+2] ) & 0xFF)
                      .arg(serial_channel.recevice_frame_waite[pos+3] & 0xFF));
      pos+=4;
      pos+=1;
      pos+=4;
      pos+=2;
      pos+=1;
      pos+=serial_channel.recevice_frame_waite[pos];
      pos+=1;
      pos+=serial_channel.recevice_frame_waite[pos];
      pos+=1;
      port = (((uchar)serial_channel.recevice_frame_waite[pos+1])<<8)+(uchar)serial_channel.recevice_frame_waite[pos];
      listemPortEdit->setText( QString("%1").arg(port));

END:
    serial_channel.need_show_flag =0;
}
void ParamSetAndRead::paramF16WidgetInit()
{
    F16UserName  = new QLabel(tr("用户名"));
    F16UserPassword = new QLabel(tr("密码"));
    F16UserNameEdit = new QLineEdit("card");
    F16UserPasswordEdit = new QLineEdit("card");

    paramF16GridLayout =new QGridLayout();

    paramF16GridLayout->addWidget(F16UserName,0,0);
    paramF16GridLayout->addWidget(F16UserNameEdit,0,1);
    paramF16GridLayout->addWidget(F16UserPassword,1,0);
    paramF16GridLayout->addWidget(F16UserPasswordEdit,1,1);
    paramF16GridLayout->setColumnStretch(2,2);
    paramF16GridLayout->setRowStretch(2,2);


    paramF16Widget = new QWidget();      //这里必须要定义一个widget，否则后面connect出问题。
    paramF16Widget->setLayout(paramF16GridLayout);
}
void ParamSetAndRead::paramF16WidgetSet()
{
    char*  ch_name;
    char* ch_password;
    QByteArray ba = F16UserNameEdit->text().toLatin1(); // must
    ch_name = ba.data();
   QByteArray  bb = F16UserPasswordEdit->text().toLatin1(); // must
    ch_password = bb.data();
    int pos =6;
     objRequest request;
     memset(request.send_frame,0,sizeof(request.send_frame));
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_SET;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F16 &0xFF;
    request.send_frame[pos++] =( DT_F16>>8) &0xFF;
    if(strlen(ch_name) <=32)
    {
        memcpy(request.send_frame+pos,ch_name,strlen(ch_name));
    }
    pos+=32;
    if(strlen(ch_password) <=32)
    {
        memcpy(request.send_frame+pos,ch_password,strlen(ch_password));
    }
     pos+=32;
     pos+=16;   //PW 部分
     request.pos = pos;
     protocol376.make376Frame(&request);
     //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF16WidgetRead()
{
     objRequest request;
         // 组报文
    int pos =6;
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_QUERY;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F16 &0xFF;
    request.send_frame[pos++] =( DT_F16>>8) &0xFF;
    request.pos = pos;
   protocol376.make376Frame(&request);
       //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF16WidgetShow( )
{
    QString username,password;
    qDebug()<< "callback is run";
    int pos = 12;
    if(serial_channel.recevice_frame_waite[pos] != 0x0A)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x80)&&(serial_channel.recevice_frame_waite[pos+1] != 0x01) )
    {
        goto END;
    }
    pos+=2;
    username.clear();
    for(int i=0;i<32;i++)
    {
        if(serial_channel.recevice_frame_waite[pos+i]!=0)
        {
            username.append(serial_channel.recevice_frame_waite[pos+i]);
        }
    }
    pos +=32;
    password.clear();
    for(int i=0;i<32;i++)
    {
        if(serial_channel.recevice_frame_waite[pos+i]!=0)
        {
            password.append(serial_channel.recevice_frame_waite[pos+i]);
        }
    }
    F16UserNameEdit->setText(username);
    F16UserPasswordEdit->setText(password);
END:
    serial_channel.need_show_flag =0;
}
void ParamSetAndRead::paramF796_16WidgetInit()
{
     paramF796_16GridLayout =new QGridLayout();
     paramF796_16Widget = new QWidget();      //这里必须要定义一个widget，否则后面connect出问题。
     F796_16_FrameMonitor_description = new QLabel(tr("报文监控选择开关"));
     F796_16FrameMonitor_gprs = new QCheckBox(tr("GPRS监控"));
     F796_16FrameMonitor_meter= new QCheckBox(tr("抄表监控"));
     F796_16_FrametoNet= new QCheckBox(tr("转发到网口"));
     F796_16_clear = new QPushButton(tr("清除选择"));
     paramF796_16GridLayout->addWidget(F796_16_FrameMonitor_description);
     paramF796_16GridLayout->addWidget(F796_16FrameMonitor_gprs);
     paramF796_16GridLayout->addWidget(F796_16FrameMonitor_meter);
     paramF796_16GridLayout->addWidget(F796_16_FrametoNet);
     paramF796_16GridLayout->addWidget(F796_16_clear);
     paramF796_16GridLayout->setColumnStretch(1,2);
     paramF796_16GridLayout->setRowStretch(5,2);
     paramF796_16Widget->setLayout(paramF796_16GridLayout);
     connect(F796_16_clear,SIGNAL(clicked()),this,SLOT(F796_16_clear_choose_Slot()));
}
void ParamSetAndRead::F796_16_clear_choose_Slot()
{
    F796_16FrameMonitor_gprs->setCheckState(Qt::Unchecked);
    F796_16FrameMonitor_meter->setCheckState(Qt::Unchecked);
}
void ParamSetAndRead::paramF796_16WidgetSet()
{
    int pos =6;
     objRequest request;
     memset(request.send_frame,0,sizeof(request.send_frame));
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_SET;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F796 &0xFF;
    request.send_frame[pos++] =( DT_F796>>8) &0xFF;

    request.send_frame[pos++]  =0x01;
    request.send_frame[pos++]  =0x10;
    request.send_frame[pos++]  =0x0;
    request.send_frame[pos++]  =0x02;
    request.send_frame[pos++]  =0x0;
    int posTmp = pos;
    pos++;
    if(F796_16FrameMonitor_gprs->isChecked())
    {
         request.send_frame[pos]  |=0x04;
    }
    if(F796_16FrameMonitor_meter->isChecked())
    {
         request.send_frame[pos]  |=0x08;
    }
    if(F796_16_FrametoNet->isChecked())
    {
         request.send_frame[pos+1]  |=0x01;
    }
    pos+=2;
     request.send_frame[posTmp] =   crc16_param( (&request.send_frame[posTmp+1]),2);
     pos+=16;   //PW 部分
     request.pos = pos;
     protocol376.make376Frame(&request);
     //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF796_16WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4B;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_QUERY;
   request.send_frame[pos++] = 0x60;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F796 &0xFF;
   request.send_frame[pos++] =( DT_F796>>8) &0xFF;
   request.send_frame[pos++]  =0x01;
   request.send_frame[pos++]  =0x10;
   request.send_frame[pos++]  =0x00;
   request.pos = pos;
   protocol376.make376Frame(&request);
      //发送到串口。
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF796_16WidgetShow()
{
    int pos = 12;
    if(serial_channel.recevice_frame_waite[pos] != 0x0A)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x08)&&(serial_channel.recevice_frame_waite[pos+1] != 0x63) )
    {
        goto END;
    }
    pos+=2;
     if((serial_channel.recevice_frame_waite[pos] != 0x01)&&(serial_channel.recevice_frame_waite[pos+1] != 0x10)\
             &&(serial_channel.recevice_frame_waite[pos+1] != 0x00)  )
     {
         goto END;
     }
      pos+=3;
      if((serial_channel.recevice_frame_waite[pos] != 0x02)&&(serial_channel.recevice_frame_waite[pos+1] != 0x00) )
      {
          goto END;
      }
      pos+=2;
      pos+=1;
    if(serial_channel.recevice_frame_waite[pos]&0x04)  //监控gprs
    {
        F796_16FrameMonitor_gprs->setChecked(TRUE);
    }
    else
    {
        F796_16FrameMonitor_gprs->setChecked(FALSE);
    }
    if(serial_channel.recevice_frame_waite[pos]&0x08)//监控抄表
    {
        F796_16FrameMonitor_meter->setChecked(TRUE);
    }
    else
    {
        F796_16FrameMonitor_meter->setChecked(FALSE);
    }
    if(serial_channel.recevice_frame_waite[pos+1]&0x01)// 监控报文转到网口
    {
        F796_16_FrametoNet->setChecked(TRUE);
    }
    else
    {
        F796_16_FrametoNet->setChecked(FALSE);
    }

END:
    serial_channel.need_show_flag =0;
}
void ParamSetAndRead::paramF796_24WidgetInit()
{
     paramF796_24Widget = new QWidget();
     paramF796_24GridLayout =new QGridLayout();
     F796_24_mes = new QLabel(tr("设备MES"));
     F796_24_mesEdit =new QLineEdit();
     F796_24_mesEdit->setFixedWidth(400);
     paramF796_24GridLayout->addWidget(F796_24_mes);
     paramF796_24GridLayout->addWidget(F796_24_mesEdit);
     paramF796_24GridLayout->setColumnStretch(1,2);
     paramF796_24GridLayout->setRowStretch(2,2);
     paramF796_24Widget->setLayout(paramF796_24GridLayout);
}
void ParamSetAndRead::paramF796_24WidgetSet()
{
    int pos =6;
     objRequest request;
     memset(request.send_frame,0,sizeof(request.send_frame));
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_SET;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F796 &0xFF;
    request.send_frame[pos++] =( DT_F796>>8) &0xFF;

    request.send_frame[pos++]  =0x01;
    request.send_frame[pos++]  =0x18;
    request.send_frame[pos++]  =0x0;
    request.send_frame[pos++]  =0x20;
    request.send_frame[pos++]  =0x0;
    int posTmp = pos;
    pos++;
    char*  mes;
    QByteArray ba = F796_24_mesEdit->text().toLatin1(); // must
    mes = ba.data();
    if(strlen(mes) <=32)  //apn 长度为16个字节。
    {
        memcpy(request.send_frame+pos,mes,strlen(mes));
    }
    pos+=32;
     request.send_frame[posTmp] =   crc16_param( (&request.send_frame[posTmp+1]),32);
     pos+=16;   //PW 部分
     request.pos = pos;
     protocol376.make376Frame(&request);
     //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF796_24WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4B;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_QUERY;
   request.send_frame[pos++] = 0x60;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F796 &0xFF;
   request.send_frame[pos++] =( DT_F796>>8) &0xFF;
   request.send_frame[pos++]  =0x01;
   request.send_frame[pos++]  =0x18;
   request.send_frame[pos++]  =0x00;
   request.pos = pos;
   protocol376.make376Frame(&request);
      //发送到串口。
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF796_24WidgetShow()
{
    QString MES;
    int pos = 12;
    if(serial_channel.recevice_frame_waite[pos] != 0x0A)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x08)&&(serial_channel.recevice_frame_waite[pos+1] != 0x63) )
    {
        goto END;
    }
    pos+=2;
     if((serial_channel.recevice_frame_waite[pos] != 0x01)&&(serial_channel.recevice_frame_waite[pos+1] != 0x18)\
             &&(serial_channel.recevice_frame_waite[pos+1] != 0x00)  )
     {
         goto END;
     }
      pos+=3;
      if((serial_channel.recevice_frame_waite[pos] != 0x20)&&(serial_channel.recevice_frame_waite[pos+1] != 0x00) )
      {
          goto END;
      }
      pos+=2;
      pos+=1;
       for(int i=0;i<32;i++)
       {
           if(serial_channel.recevice_frame_waite[pos+i]!=0x00)
           {
                MES.append(serial_channel.recevice_frame_waite[pos+i]);
           }
       }
       F796_24_mesEdit->setText(MES);
END:
    serial_channel.need_show_flag =0;
}
//设置报文监控开关 F799GPRS 扩展信息  IMEI   ICCID
void ParamSetAndRead::paramF799WidgetInit()
{
     paramF799Widget  = new QWidget();
     paramF799GridLayout =new QGridLayout();
    F799_IMEI = new QLabel(tr("IMEI: "));
    F799_ICCID= new QLabel(tr("ICCID: "));
    COUNTER_ADDR= new QLabel(tr("反制模块地址: "));
    F799_IMEI_Edit = new QLineEdit("");
    F799_IMEI_Edit->setReadOnly(TRUE);
    F799_ICCID_Edit = new QLineEdit();
    F799_ICCID_Edit->setReadOnly(TRUE);
    COUNTER_ADDR_Edit = new QLineEdit();
    COUNTER_ADDR_Edit->setReadOnly(TRUE);
     COUNTER_ADDR_Edit->setFixedWidth(400);

    paramF799GridLayout->addWidget(F799_IMEI,0,0);
    paramF799GridLayout->addWidget(F799_IMEI_Edit,0,1);
    paramF799GridLayout->addWidget(F799_ICCID,1,0);
    paramF799GridLayout->addWidget(F799_ICCID_Edit,1,1);
    paramF799GridLayout->addWidget(COUNTER_ADDR,2,0);
    paramF799GridLayout->addWidget(COUNTER_ADDR_Edit,2,1);
    paramF799GridLayout->setColumnStretch(2,2);
    paramF799GridLayout->setRowStretch(3,2);
    paramF799Widget->setLayout(paramF799GridLayout);
}
void ParamSetAndRead::paramF799WidgetSet()
{

}
void ParamSetAndRead::paramF799WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4B;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_READCUR;
   request.send_frame[pos++] = 0xEA;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F799 &0xFF;
   request.send_frame[pos++] =( DT_F799>>8) &0xFF;
   pos+=6;
   request.pos = pos;
  protocol376.make376Frame(&request);
      //发送到串口。
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();
}
void ParamSetAndRead::paramF799WidgetShow()
{
    typedef union{
       INT8U value[0];
        struct{
          INT8U csq;           //信号强度
          INT8U ber;           //误码率
          INT8U op;            //当前操作
          INT8U gprs_end_state;         //最终状态
          char          temp;          //当前温度
          INT8U mac[6];        //mac地址
          INT8U g14_ver[8];    //g14版本信息，V3.0没有
          INT8U ctrl_ver[5];   //控制模块版本信息
          INT8U soft_ver[7];   //内部版本
          INT8U provice[3];   //省份特征字，V3.0暂时没有
          INT8U ram_status;   //内存是否损坏,AA-正常 0-损坏
          INT8U osc_status;   //主晶振是否异常，0xAA-正常 1-异常
          INT8U rtcc_status;   //RTCC工作状态，0xAA-正常 1-异常
          INT8U error_status;//D0-交采通讯故障,1为故障 D1-时钟故障状态 D2-蓝牙通讯故障 D3-ESAM通讯故障
          INT8U check_key;                //工装检测按键bit7-2：6个按键（1检测到，0未检测到），bit1-0: 01 开启按键检测；
          INT8U reserved[5];      //保留
          INT8U sim_id[20];    //SIM卡ID
          INT8U djb_ver[16]; //蓝牙版本
          INT8U gprs_type[3];//模块类型，字符
          INT8U gprs_imsi[15];//imsi
          INT8U config_ver[2];           //配置脚本文件版本
          INT8U config_provice[4];       //配置脚本文件省份代码
          INT8U bad_block_count[2];       //Flash中坏块数量
          INT8U version[23];          //模组固件版本
          INT8U imei[19];				//IMEI
       }a;
    }tagWORKINFO   ;
    tagWORKINFO workInfo;
    QString IMEI,ICCID;
    qDebug()<< "callback is run";
    int pos = 12;
    if(serial_channel.recevice_frame_waite[pos] != 0x0C)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x40)&&(serial_channel.recevice_frame_waite[pos+1] != 0x63) )
    {
        goto END;
    }
    pos+=2;
    memcpy((INT8U *)&workInfo.value,serial_channel.recevice_frame_waite+pos,sizeof(tagWORKINFO));
    for(int i=0;i< 15;i++)
    {
        IMEI.append(workInfo.a.imei[4+i]);
    }
    for(int i=0;i< 20;i++)
    {
        ICCID.append(workInfo.a.sim_id[i]);
    }
    F799_IMEI_Edit->setText(IMEI);

    F799_ICCID_Edit->setText(ICCID);
#ifndef HONGHAO212
    COUNTER_ADDR_Edit->setText("0"+IMEI+ICCID);
#endif
END:
    serial_channel.need_show_flag =0;
}

 void ParamSetAndRead::Update376_WidgetInit()
{
       Update376Widget = new QWidget();
       Update376GridLayout = new QGridLayout();
       Update376FilePathLabel = new QLabel(tr("升级文件路径"));
       Update376FileButton = new QPushButton(tr("打开升级文件"));
       Update376FilePath_Edit  =new QLineEdit();
       Update376Start= new QPushButton(tr("开始升级"));
       Update376End= new QPushButton(tr("结束/清除升级"));
       progressbar = new QProgressBar();
       progressbar->setRange(0,100);
       progressbar->setValue(0);
       Update376GridLayout->addWidget(Update376FilePathLabel,0,0);
       Update376GridLayout->addWidget(Update376FilePath_Edit,0,1);
       Update376GridLayout->addWidget(Update376FileButton,1,1);
       Update376GridLayout->addWidget(Update376Start,2,0);
       Update376GridLayout->addWidget(Update376End,2,1);
       Update376GridLayout->addWidget(progressbar,3,0,1,4);
       Update376GridLayout->setColumnStretch(4,2);
       Update376GridLayout->setRowStretch(4,2);
       Update376Widget->setLayout(Update376GridLayout);
       connect(Update376FileButton,SIGNAL(clicked()),this,SLOT(update_file_broswer_slot()));

       connect(Update376Start,SIGNAL(clicked()),this,SLOT(update_start_slot()));
       connect(Update376End,SIGNAL(clicked()),this,SLOT(update_close_slot()));

}
 void ParamSetAndRead::update_file_broswer_slot()
{
     Update376FilePath_Edit->clear();
     fileName.clear();
     updateFileLen =0;
     fileName = QFileDialog::getOpenFileName(
         this,
         tr("浏览升级文件"),
         "D:/",
         tr("updatefile(*.dat);;images(*.png *jpeg *bmp);;video files(*.avi *.mp4 *.wmv);;All files(*.*)"));
     if (fileName.isEmpty()) {
         QMessageBox::warning(this, "Warning!", "升级文件没有找到");
     }
     else{
        Update376FilePath_Edit->setText(fileName);
         updateFileHandler.setFileName(fileName);
        updateFileHandler.open(QIODevice::ReadOnly);
        QByteArray q = updateFileHandler.readAll();
       // updateFile = (INT8U*) q.data();
        memcpy((INT8U *)updateFile,(INT8U *)q.data(),q.size());
        updateFileLen = q.size();
        file_total_seg = updateFileLen/1024+1;
        file_cur_seg =0;
        qDebug()<<"file total seg is "<<file_total_seg;
     }
  }
 void ParamSetAndRead::make_376update_frame()
 {
     objRequest request;
     int update_date_len =1024;
     bool  end_seg=FALSE;
     if(file_cur_seg*1024 >updateFileLen)
     {
         intoUpdateState =0;
         progressbar->setValue(100);
         QMessageBox::warning(this, "Warning!", "升级完成");
        return ;
     }
     if((file_cur_seg+1)*1024>updateFileLen)
     {
         update_date_len = updateFileLen -(file_cur_seg)*1024;
         end_seg =TRUE;
     }
         // 组报文
    int pos =6;
     // 组报文
    request.send_frame[pos++] = 0x4B;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_FTP;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F1 &0xFF;
    request.send_frame[pos++] =( DT_F1>>8) &0xFF;
    //pos+=6;
    request.send_frame[pos++] = 0xFA;
    if(end_seg)
    {
         request.send_frame[pos++] = 0x1;//结束帧。
    }
    else
    {
         request.send_frame[pos++] = 0x0;//开始帧。
    }
    request.send_frame[pos++] = 0x0;
    request.send_frame[pos++] = file_total_seg&0xFF;
    request.send_frame[pos++] = (file_total_seg>>8)&0xFF;
    request.send_frame[pos++] = file_cur_seg&0xFF;
    request.send_frame[pos++] = (file_cur_seg>>8)&0xFF;
    request.send_frame[pos++] = (file_cur_seg>>16)&0xFF;
    request.send_frame[pos++] = (file_cur_seg>>24)&0xFF;
    request.send_frame[pos++] =update_date_len&0xFF;
    request.send_frame[pos++] =(update_date_len>>8)&0xFF;
    for(int i=0;i<update_date_len;i++)  //逆序
    {
        request.send_frame[pos++] = updateFile[file_cur_seg*1024+(update_date_len-1)-i];
    }
    //memcpy(request.send_frame+pos,updateFile+file_cur_seg*1024 ,update_date_len);
    request.pos = pos;
   protocol376.make376Frame(&request);
       //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
     serial_channel.need_show_flag=1;
     serial_channel.need_show_AFN =0xF;
     emit sendframeSignal_param_page();
     intoUpdateState =0xAA;
 }
 void ParamSetAndRead::update_start_slot()
 {
    qDebug()<<"start button ";
    if (updateFileLen !=0) {  //有合法的升级文件

        make_376update_frame();

    }
 }
 void ParamSetAndRead::update_close_slot()
 {
    qDebug()<<"close button ";
    file_cur_seg =0;
 }
void ParamSetAndRead::paramWriteSlot()
{
    switch(paramList->currentRow())
    {
            case 0:  //读版本。
                break;
            case 1:   //设置 F3
                 paramF3WidgetSet();
                 serial_channel.need_show_flag =FALSE;
                break;
           case 2:  //设置 F7
                paramF7WidgetSet();
                 serial_channel.need_show_flag =FALSE;
                break;
           case 3:  //设置F16
                paramF16WidgetSet();
                serial_channel.need_show_flag =FALSE;
                break;
            case 5:
                paramF796_16WidgetSet();
                serial_channel.need_show_flag =FALSE;
                break;
           case 6:  //设置 F796  mes码
                if(passwordInput->text()=="TOPS")
                {
                    paramF796_24WidgetSet();
                    serial_channel.need_show_flag =FALSE;
                }
            break;
        case 8:   //设置 F803
             paramF803WidgetSet();
             serial_channel.need_show_flag =FALSE;
        break;
        case 9:   //设置 F796 -182
             paramF796_182WidgetSet();
             serial_channel.need_show_flag =FALSE;
        break;
    case 10:   //设置 F796 -1
         paramF796_1WidgetSet();
         serial_channel.need_show_flag =FALSE;
         break;
    case 11:   //设置 F796 -18_36
         paramF796_18_36WidgetSet();
         serial_channel.need_show_flag =FALSE;
    break;
           default:
               qDebug()<<"错误的选择，重试";
                break;
    }
}
void ParamSetAndRead::paramReadSlot()
{
    qDebug()<<" read this currentRow is "+QString::number(paramList->currentRow());
    int list_row = paramList->currentRow();
    switch(list_row)
    {
            case 0 ://  读09F1
                param09F1WidgetRead();
                serial_channel.need_show_flag =TRUE;
                serial_channel.need_show_AFN = 0x9;
                serial_channel.need_show_FN =1;
                break;
            case 1:   //读取 F3
                paramF3WidgetRead();
                serial_channel.need_show_flag =TRUE;
                serial_channel.need_show_AFN = 0xA;
                serial_channel.need_show_FN =3;
                break;
           case 2:  //读取 F7
                paramF7WidgetRead();
                serial_channel.need_show_flag =TRUE;
                serial_channel.need_show_AFN = 0xA;
                serial_channel.need_show_FN =7;
                break;
           case 3:  //读取 F16
               paramF16WidgetRead();
               serial_channel.need_show_flag =TRUE;
               serial_channel.need_show_AFN = 0xA;
               serial_channel.need_show_FN =16;
                break;
            case 4:  //读取 F799 imei iccid
                    paramF799WidgetRead();
                    serial_channel.need_show_flag =TRUE;
                    serial_channel.need_show_AFN = 0xC;
                    serial_channel.need_show_FN =799;
                     break;
            case 5:    //读取 F796   报文监控开关
                    paramF796_16WidgetRead();
                    serial_channel.need_show_flag =TRUE;
                    serial_channel.need_show_AFN = 0xA;
                    serial_channel.need_show_FN =796;
                    serial_channel.need_show_subFn = 16;
                     break;
            case 6:  //读取 F796  mes码
                    if(passwordInput->text()=="TOPS")
                    {
                        paramF796_24WidgetRead();
                        serial_channel.need_show_flag =TRUE;
                        serial_channel.need_show_AFN = 0xA;
                        serial_channel.need_show_FN =796;
                         serial_channel.need_show_subFn = 24;
                    }
                     break;
         case 8:  //读取 803码
            paramF803WidgetRead();
            serial_channel.need_show_flag =TRUE;
            serial_channel.need_show_AFN = 0xA;
            serial_channel.need_show_FN =803;
             break;
    case 9:  //读取 F796  MN码
                paramF796_182WidgetRead();
                serial_channel.need_show_flag =TRUE;
                serial_channel.need_show_AFN = 0xA;
                serial_channel.need_show_FN =796;
                 serial_channel.need_show_subFn = 182;
             break;
    case 10:  //读取 F796  1 终端地址码
                paramF796_1WidgetRead();
                serial_channel.need_show_flag =TRUE;
                serial_channel.need_show_AFN = 0xA;
                serial_channel.need_show_FN =796;
                 serial_channel.need_show_subFn = 1;
             break;
    case 11:  //读取 F796  电压电流倍率
                paramF796_18_36WidgetRead();
                serial_channel.need_show_flag =TRUE;
                serial_channel.need_show_AFN = 0xA;
                serial_channel.need_show_FN =796;
                 serial_channel.need_show_subFn = 18;
             break;
           default:
               qDebug()<<"错误的选择，重试";
                break;
    }
}

void ParamSetAndRead::  F3ParamRestoreFun()
{
#ifdef      HONGHAO212
    F3MainIPEdit->setText("218.11.13.63");
    F3MainPortEdit->setText("5010");
    F3SecondIPEdit->setText("121.17.30.65");
    F3SecondPortEdit->setText("9606");
    F3APNEdit->setText("cmnet");
#endif
}
void ParamSetAndRead::  F803ParamRestoreFun()
{
 #ifdef      HONGHAO212
    F803MainIPEdit->setText("121.17.30.65");
    F803MainPortEdit->setText("9502");
    F803SecondIPEdit->setText("8.140.40.74");
    F803SecondPortEdit->setText("60086");
    F803APNEdit->setText("cmnet");
#endif
}
void ParamSetAndRead:: paramF803WidgetInit()
{
    //网址判断。
    QRegularExpression exp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
    QRegularExpressionValidator *ipVal = new QRegularExpressionValidator(exp);
    paramF803GridLayout =new QGridLayout();
#ifdef HONGHAO212
    F803MainIP = new QLabel(tr("市环保平台 IP"));
#else
   F803MainIP = new QLabel(tr("主用 IP"));
#endif
   F803MainPort = new QLabel(tr("端口"));
#ifdef HONGHAO212
   F803SecondIP = new QLabel(tr("鸿浩控制平台IP"));
#else
  F803SecondIP = new QLabel(tr("备用 IP"));
#endif
   F803SecondPort = new QLabel(tr("端口"));
   F803APN = new QLabel(tr("APN"));
#ifdef HONGHAO212
   F803MainIPEdit=new QLineEdit("121.17.30.65");
#else
     F803MainIPEdit=new QLineEdit();
   #endif
    F3MainIPEdit->setInputMask("000.000.000.000");
    F803MainIPEdit->setEchoMode(QLineEdit::Normal);
    F803MainIPEdit->setEnabled(true);
#ifdef     HONGHAO212
    F803MainPortEdit=new QLineEdit("9502");
#else
    F803MainPortEdit=new QLineEdit();
   #endif
    F803MainPortEdit->setInputMask("0000");

    F803MainPortEdit->setMaxLength(5);
    F803MainPortEdit->setEchoMode(QLineEdit::Normal);
    F803MainPortEdit->setEnabled(true);
#ifdef     HONGHAO212
    F803SecondIPEdit=new QLineEdit("8.140.40.74");
#else
       F803SecondIPEdit=new QLineEdit();
#endif
     F803SecondIPEdit->setInputMask("000.000.000.000");
     F803SecondIPEdit->setEchoMode(QLineEdit::Normal);
     F803SecondIPEdit->setEnabled(true);
#ifdef     HONGHAO212
     F803SecondPortEdit=new QLineEdit("60086");
#else
     F803SecondPortEdit=new QLineEdit();
#endif
       F803SecondPortEdit->setInputMask("00000");
     F803SecondPortEdit->setMaxLength(5);
    F803APNEdit=new QLineEdit("cmnet");
    F803ParamRestore = new QPushButton(tr("默认参数恢复"));
    paramF803GridLayout->addWidget(F803MainIP,0,0);
    paramF803GridLayout->addWidget(F803MainIPEdit,0,1);
    paramF803GridLayout->addWidget(F803MainPort,0,2);
    paramF803GridLayout->addWidget(F803MainPortEdit,0,3);

    paramF803GridLayout->addWidget(F803SecondIP,1,0);
    paramF803GridLayout->addWidget(F803SecondIPEdit,1,1);
    paramF803GridLayout->addWidget(F803SecondPort,1,2);
    paramF803GridLayout->addWidget(F803SecondPortEdit,1,3);

    paramF803GridLayout->addWidget(F803APN,2,0);
    paramF803GridLayout->addWidget(F803APNEdit,2,1);
    paramF803GridLayout->addWidget(F803ParamRestore,3,0);
    paramF803GridLayout->setColumnStretch(4,2);
    paramF803GridLayout->setRowStretch(4,2);
     paramF803Widget = new QWidget();      //这里必须要定义一个widget，否则后面connect出问题。
    paramF803Widget->setLayout(paramF803GridLayout);
     connect (F803ParamRestore,SIGNAL(clicked()),this,SLOT(F803ParamRestoreFun()));
}
void ParamSetAndRead:: paramF803WidgetSet()
{
    quint32 MainIP,SecondIP,MianPort,secondPort;
    QStringList ips = F803MainIPEdit->text().split(".");
    if(ips.size() == 4){
        MainIP =   ips.at(0).toInt()
                | ips.at(1).toInt() << 8
                | ips.at(2).toInt() << 16
                | ips.at(3).toInt() << 24;
    }
   ips = F803SecondIPEdit->text().split(".");
    if(ips.size() == 4){
        SecondIP =   ips.at(0).toInt()
                | ips.at(1).toInt() << 8
                | ips.at(2).toInt() << 16
                | ips.at(3).toInt() << 24;
    }
    MianPort = F803MainPortEdit->text().toInt();
    secondPort = F803SecondPortEdit->text().toInt();
    int pos =6;
     objRequest request;
     memset(request.send_frame,0,sizeof(request.send_frame));
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_SET;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F803 &0xFF;
    request.send_frame[pos++] =( DT_F803>>8) &0xFF;
    request.send_frame[pos++] =0x00;   //表示第几个
    memcpy(request.send_frame+pos,(INT8U*)&MainIP,4);
    pos+=4;
    memcpy(request.send_frame+pos,(INT8U*)&MianPort,2);
    pos+=2;
    memcpy(request.send_frame+pos,(INT8U*)&SecondIP,4);
    pos+=4;
    memcpy(request.send_frame+pos,(INT8U*)&secondPort,2);
    pos+=2;
    char*  apn;
    QByteArray ba = F803APNEdit->text().toLatin1(); // must
    apn = ba.data();
    if(strlen(apn) <=16)  //apn 长度为16个字节。
    {
        memcpy(request.send_frame+pos,apn,strlen(apn));
    }
    pos+=16;
     pos+=16;   //PW 部分
     request.pos = pos;
     protocol376.make376Frame(&request);
     //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}
void ParamSetAndRead:: paramF803WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4A;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_QUERY;
   request.send_frame[pos++] = 0x60;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F803 &0xFF;
   request.send_frame[pos++] =( DT_F803>>8) &0xFF;
   request.send_frame[pos++] =0x00;
   request.pos = pos;
   protocol376.make376Frame(&request);
      //发送到串口。
   F803MainIPEdit->clear();
   F803MainPortEdit->clear();
   F803SecondIPEdit->clear();
   F803SecondPortEdit->clear();
    F803APNEdit->clear();
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();
}
void ParamSetAndRead:: paramF803WidgetShow()
{
    QString  apn;
     int mainPort,secondPort;
    int pos = 12;
    if(serial_channel.recevice_frame_waite[pos] != 0x0A)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x04)&&(serial_channel.recevice_frame_waite[pos+1] != 0x00) )
    {
        goto END;
    }
    pos+=2;
    F803MainIPEdit->setText( QString("%1.%2.%3.%4")
                .arg((serial_channel.recevice_frame_waite[pos] ) & 0xFF)
                .arg((serial_channel.recevice_frame_waite[pos+1] ) & 0xFF)
                .arg((serial_channel.recevice_frame_waite[pos+2] ) & 0xFF)
                .arg(serial_channel.recevice_frame_waite[pos+3] & 0xFF));
    pos+=4;
    mainPort = ((int((uchar )serial_channel.recevice_frame_waite[pos+1]))<<8) +(uchar)serial_channel.recevice_frame_waite[pos] ;
    F803MainPortEdit->setText(QString("%1").arg(mainPort));
    pos+=2;
    F803SecondIPEdit->setText( QString("%1.%2.%3.%4")
                   .arg((serial_channel.recevice_frame_waite[pos] ) & 0xFF)
                   .arg((serial_channel.recevice_frame_waite[pos+1] ) & 0xFF)
                   .arg((serial_channel.recevice_frame_waite[pos+2] ) & 0xFF)
                   .arg(serial_channel.recevice_frame_waite[pos+3] & 0xFF));
      pos+=4;
      secondPort = ((uint((uchar )serial_channel.recevice_frame_waite[pos+1]))<<8) +(uchar)serial_channel.recevice_frame_waite[pos] ;
      F803SecondPortEdit->setText(QString("%1").arg(secondPort));
      pos+=2;
      for(int i=0;i<16;i++)
      {
          if(serial_channel.recevice_frame_waite[pos+i]!=0)
          {
              apn.append(serial_channel.recevice_frame_waite[pos+i]);
          }
      }
      F803APNEdit->setText(apn);
END:
    serial_channel.need_show_flag =0;
}
void ParamSetAndRead::  paramF796_182WidgetInit()
{
    paramF796_182Widget = new QWidget();
    paramF796_182GridLayout =new QGridLayout();
    F796_182_MN = new QLabel(tr("设备MN号"));
    F796_182_MNEdit =new QLineEdit();
    F796_182_MNEdit->setFixedWidth(400);
    paramF796_182GridLayout->addWidget(F796_182_MN);
    paramF796_182GridLayout->addWidget(F796_182_MNEdit);
    paramF796_182GridLayout->setColumnStretch(1,2);
    paramF796_182GridLayout->setRowStretch(2,2);
    paramF796_182Widget->setLayout(paramF796_182GridLayout);
}
void ParamSetAndRead::  paramF796_182WidgetSet()
{
    int pos =6;
     objRequest request;
     memset(request.send_frame,0,sizeof(request.send_frame));
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_SET;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F796 &0xFF;
    request.send_frame[pos++] =( DT_F796>>8) &0xFF;

    request.send_frame[pos++]  =0x01;
    request.send_frame[pos++]  =0xB6;
    request.send_frame[pos++]  =0x0;
    request.send_frame[pos++]  =0x20;
    request.send_frame[pos++]  =0x0;
    int posTmp = pos;
    pos++;
    char*  MN;
    QByteArray ba = F796_182_MNEdit->text().toLatin1(); // must
    MN = ba.data();
    if(strlen(MN) ==24)  //apn 长度为16个字节。
    {
        memcpy(request.send_frame+pos,MN,strlen(MN));
    }
    else
    {
        QMessageBox::StandardButton reply;
         reply = QMessageBox::information(this,  "QMessageBox::information()","输入的MN长度不对",QMessageBox::Ok);
        return;
    }
    pos+=32;
     request.send_frame[posTmp] =   crc16_param( (&request.send_frame[posTmp+1]),32);
     pos+=16;   //PW 部分
     request.pos = pos;
     protocol376.make376Frame(&request);
     //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}
void ParamSetAndRead::  paramF796_182WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4B;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_QUERY;
   request.send_frame[pos++] = 0x60;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F796 &0xFF;
   request.send_frame[pos++] =( DT_F796>>8) &0xFF;
   request.send_frame[pos++]  =0x01;
   request.send_frame[pos++]  =0xB6;
   request.send_frame[pos++]  =0x00;
   request.pos = pos;
   protocol376.make376Frame(&request);
      //发送到串口。
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();
}
void ParamSetAndRead::  paramF796_182WidgetShow()
{
    QString MN;
    int pos = 12;
    if(serial_channel.recevice_frame_waite[pos] != 0x0A)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x08)&&(serial_channel.recevice_frame_waite[pos+1] != 0x63) )
    {
        goto END;
    }
    pos+=2;
     if((serial_channel.recevice_frame_waite[pos] != 0x01)&&(serial_channel.recevice_frame_waite[pos+1] != 0xB6)\
             &&(serial_channel.recevice_frame_waite[pos+1] != 0x00)  )
     {
         goto END;
     }
      pos+=3;
      if((serial_channel.recevice_frame_waite[pos] != 0x18)&&(serial_channel.recevice_frame_waite[pos+1] != 0x00) )
      {
          goto END;
      }
      pos+=2;
      pos+=1;
       for(int i=0;i<24;i++)
       {
           if(serial_channel.recevice_frame_waite[pos+i]!=0x00)
           {
                MN.append(serial_channel.recevice_frame_waite[pos+i]);
           }
       }
       F796_182_MNEdit->setText(MN);
END:
    serial_channel.need_show_flag =0;
}
void ParamSetAndRead:: paramF796_1WidgetInit()
{
    paramF796_1Widget = new QWidget();
    paramF796_1GridLayout =new QGridLayout();
#ifdef      HONGHAO212
    F796_1_ERTU_ADDR = new QLabel(tr("设备tn号"));
#else
    F796_1_ERTU_ADDR = new QLabel(tr("设备终端地址"));
#endif
   F796_1_ERTU_ADDREdit =new QLineEdit();
   F796_1_ERTU_ADDREdit->setFixedWidth(400);
    paramF796_1GridLayout->addWidget(F796_1_ERTU_ADDR);
    paramF796_1GridLayout->addWidget(F796_1_ERTU_ADDREdit);
    paramF796_1GridLayout->setColumnStretch(1,2);
    paramF796_1GridLayout->setRowStretch(2,2);
    paramF796_1Widget->setLayout(paramF796_1GridLayout);
}
void ParamSetAndRead::  paramF796_1WidgetSet()
{
    int pos =6;
     objRequest request;
     memset(request.send_frame,0,sizeof(request.send_frame));
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_SET;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F796 &0xFF;
    request.send_frame[pos++] =( DT_F796>>8) &0xFF;

    request.send_frame[pos++]  =0x01;
    request.send_frame[pos++]  =0x01;
    request.send_frame[pos++]  =0x0;
    request.send_frame[pos++]  =0x04;
    request.send_frame[pos++]  =0x0;
    int posTmp = pos;
    pos++;
    char*  MN;
    QByteArray ba = F796_1_ERTU_ADDREdit->text().toLatin1(); // must
    MN = ba.data();
    int  addr = atoi(MN);

    request.send_frame[pos++]  =0x02;
    request.send_frame[pos++]  =0x61;

    request.send_frame[pos++]  =addr&0xFF;
    request.send_frame[pos++]  =(addr>>8)&0xFF;
     request.send_frame[posTmp] =   crc16_param( (&request.send_frame[posTmp+1]),4);
     pos+=16;   //PW 部分
     request.pos = pos;
     protocol376.make376Frame(&request);
     //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}

void ParamSetAndRead::  paramF796_1WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4B;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_QUERY;
   request.send_frame[pos++] = 0x60;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F796 &0xFF;
   request.send_frame[pos++] =( DT_F796>>8) &0xFF;
   request.send_frame[pos++]  =0x01;
   request.send_frame[pos++]  =0x01;
   request.send_frame[pos++]  =0x00;
   request.pos = pos;
   protocol376.make376Frame(&request);
      //发送到串口。
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();
}

void ParamSetAndRead:: paramF796_1WidgetShow()
{
    QString MN;
    int pos = 12;
    int addr  = 0;
    if(serial_channel.recevice_frame_waite[pos] != 0x0A)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x08)&&(serial_channel.recevice_frame_waite[pos+1] != 0x63) )
    {
        goto END;
    }
    pos+=2;
     if((serial_channel.recevice_frame_waite[pos] != 0x01)&&(serial_channel.recevice_frame_waite[pos+1] != 0x01)\
             &&(serial_channel.recevice_frame_waite[pos+1] != 0x00)  )
     {
         goto END;
     }
      pos+=3;
      if((serial_channel.recevice_frame_waite[pos] != 0x04)&&(serial_channel.recevice_frame_waite[pos+1] != 0x00) )
      {
          goto END;
      }
      pos+=2;
      pos+=1;
      pos+=2;
      addr  = uint((uchar )serial_channel.recevice_frame_waite[pos]) +(uint((uchar )serial_channel.recevice_frame_waite[pos+1]<<8));
       F796_1_ERTU_ADDREdit->setText( QString::number(addr));
END:
    serial_channel.need_show_flag =0;
}
void ParamSetAndRead:: paramF796_18_36WidgetInit()
{
    paramF796_18_36Widget = new QWidget();
    paramF796_18_36GridLayout =new QGridLayout();

    F796_18_36Voltage = new QLabel(tr("电压倍率"));
    F796_18_36Current = new QLabel(tr("电流倍率"));
   F796_18_36VoltageEdit =new QLineEdit("1");
   F796_18_36CurrentEdit = new QLineEdit("1");

    paramF796_18_36GridLayout->addWidget(F796_18_36Voltage,0,0);
    paramF796_18_36GridLayout->addWidget(F796_18_36VoltageEdit,0,1);

    paramF796_18_36GridLayout->addWidget(F796_18_36Current,1,0);
    paramF796_18_36GridLayout->addWidget(F796_18_36CurrentEdit,1,1);
    paramF796_18_36GridLayout->setColumnStretch(2,1);
    paramF796_18_36GridLayout->setRowStretch(2,1);
    paramF796_18_36Widget->setLayout(paramF796_18_36GridLayout);
}
void ParamSetAndRead::  paramF796_18_36WidgetSet()
{
    int pos =6;
     objRequest request;
     memset(request.send_frame,0,sizeof(request.send_frame));
     // 组报文
    request.send_frame[pos++] = 0x4A;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0xFF;
    request.send_frame[pos++] = 0x14;
    request.send_frame[pos++] =AFN_SET;
    request.send_frame[pos++] = 0x60;   //SEQ
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = 0x00;
    request.send_frame[pos++] = DT_F796 &0xFF;
    request.send_frame[pos++] =( DT_F796>>8) &0xFF;
//
    request.send_frame[pos++]  =0x02;
    request.send_frame[pos++]  =0x12;
    request.send_frame[pos++]  =0x0;
    request.send_frame[pos++]  =0x04;
    request.send_frame[pos++]  =0x0;
    int posTmp = pos;
    pos++;
    char*  MN;
    QByteArray ba = F796_18_36VoltageEdit->text().toLatin1(); // must
    MN = ba.data();
    int  voltage = atoi(MN);
    if(voltage==0)
    {
        voltage =1;
    }
    request.send_frame[pos++]  =voltage&0xFF;
    request.send_frame[pos++]  =(voltage>>8)&0xFF;
    request.send_frame[pos++]  =(voltage>>16)&0xFF;
    request.send_frame[pos++]  =(voltage>>24)&0xFF;
   request.send_frame[posTmp] =   crc16_param( (&request.send_frame[posTmp+1]),4);
//电流
    request.send_frame[pos++]  =0x24;
    request.send_frame[pos++]  =0x0;
    request.send_frame[pos++]  =0x04;
    request.send_frame[pos++]  =0x0;
    posTmp = pos;
    pos++;
    ba = F796_18_36CurrentEdit->text().toLatin1(); // must
    MN = ba.data();
    int  current = atoi(MN);
    if(current==0)
    {
        current =1;
    }
    request.send_frame[pos++]  =current&0xFF;
    request.send_frame[pos++]  =(current>>8)&0xFF;
    request.send_frame[pos++]  =(current>>16)&0xFF;
    request.send_frame[pos++]  =(current>>24)&0xFF;

     request.send_frame[posTmp] =   crc16_param( (&request.send_frame[posTmp+1]),4);
     pos+=16;   //PW 部分
     request.pos = pos;
     protocol376.make376Frame(&request);
     //发送到串口。
     memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
     serial_channel.response_len = request.frame_len;
      emit sendframeSignal_param_page();
}
void ParamSetAndRead::  paramF796_18_36WidgetRead()
{
    objRequest request;
        // 组报文
   int pos =6;
    // 组报文
   request.send_frame[pos++] = 0x4B;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0xFF;
   request.send_frame[pos++] = 0x14;
   request.send_frame[pos++] =AFN_QUERY;
   request.send_frame[pos++] = 0x60;   //SEQ
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = 0x00;
   request.send_frame[pos++] = DT_F796 &0xFF;
   request.send_frame[pos++] =( DT_F796>>8) &0xFF;
   request.send_frame[pos++]  =0x02;
   request.send_frame[pos++]  =0x12;
   request.send_frame[pos++]  =0x00;
   request.send_frame[pos++]  =0x24;
   request.send_frame[pos++]  =0x00;
   request.pos = pos;
   protocol376.make376Frame(&request);
      //发送到串口。
    memcpy(serial_channel.response_frame,request.send_frame,request.frame_len);
    serial_channel.response_len = request.frame_len;
     emit sendframeSignal_param_page();

     F796_18_36VoltageEdit->clear();
     F796_18_36CurrentEdit->clear();
}
void ParamSetAndRead::  paramF796_18_36WidgetShow()
{
    QString MN;
    int pos = 12;
    int tmp  = 0;
    if(serial_channel.recevice_frame_waite[pos] != 0x0A)
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x0)&&(serial_channel.recevice_frame_waite[pos+1] != 0x0) )
    {
        goto END;
    }
    pos+=2;
    if((serial_channel.recevice_frame_waite[pos] != 0x08)&&(serial_channel.recevice_frame_waite[pos+1] != 0x63) )
    {
        goto END;
    }
    pos+=2;
     if((serial_channel.recevice_frame_waite[pos] != 0x02)&&(serial_channel.recevice_frame_waite[pos+1] != 0x12)\
             &&(serial_channel.recevice_frame_waite[pos+1] != 0x00)  )
     {
         goto END;
     }
      pos+=3;
      if((serial_channel.recevice_frame_waite[pos] != 0x04)&&(serial_channel.recevice_frame_waite[pos+1] != 0x00) )
      {
          goto END;
      }
      pos+=2;
      pos+=1;
      tmp  = uint((uchar )serial_channel.recevice_frame_waite[pos]) +(uint((uchar )serial_channel.recevice_frame_waite[pos+1]<<8))\
              +(uint((uchar )serial_channel.recevice_frame_waite[pos+2]<<16))+(uint((uchar )serial_channel.recevice_frame_waite[pos+3]<<24));
       F796_18_36VoltageEdit->setText( QString::number(tmp));
  pos+=4;
       if( (serial_channel.recevice_frame_waite[pos] != 0x24)\
               &&(serial_channel.recevice_frame_waite[pos+1] != 0x00)  )
       {
           goto END;
       }
        pos+=2;
        if((serial_channel.recevice_frame_waite[pos] != 0x04)&&(serial_channel.recevice_frame_waite[pos+1] != 0x00) )
        {
            goto END;
        }
        pos+=2;
        pos+=1;
        tmp  = uint((uchar )serial_channel.recevice_frame_waite[pos]) +(uint((uchar )serial_channel.recevice_frame_waite[pos+1]<<8))\
                +(uint((uchar )serial_channel.recevice_frame_waite[pos+2]<<16))+(uint((uchar )serial_channel.recevice_frame_waite[pos+3]<<24));
         F796_18_36CurrentEdit->setText( QString::number(tmp));

END:
    serial_channel.need_show_flag =0;
}
